﻿<div id="pageBox">
    <div class="box">
        <div class="box-header">
            <h3 class="box-title">Role List</h3>
            <div class="box-tools">
                <button data-toggle="modal" data-target="#modal-role" type="button" title="Add Roleure" class="btn btn-xs btn-info">
                    <i class="fa fa-plus"></i> Add Role
                </button>
            </div>
        </div>
        <div class="box-body no-padding">
            <div class="dataTables_wrapper form-inline dt-bootstrap">
                <table id="mainTable" ref="mainTable" class="table table-striped" style="width:100%;">
                </table>
            </div>
        </div>
    </div>

    <!--修改增加Modal-->
    <div class="modal fade" id="modal-role">
        <input id="role_id" value="0" type="hidden" />       
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <h4 class="modal-title">Edit Role</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>Name</label>
                        <input id="role_name" v-model="info.Name"  type="text" class="form-control" placeholder="Enter Name...">
                    </div>
                    <div class="form-group">
                        <label>Remark</label>
                        <input id="remark" v-model="info.Remark" type="text" class="form-control" placeholder="Enter Remark...">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" v-on:click="Save()">Save changes</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>
@section Styles{
    
}
@section Scripts{
    <script type="text/javascript">

    var MyVue = new Vue({
        el: "#pageBox",
        data: {
            roles: [],
            info:[]
        },
        mounted: function () {
            $this = this;
            $this.LoadData();
            $this.Modal();
        },
        methods: {
            IntTable: function () {
                $this = this;
                $('#mainTable').DataTable({
                    searching: false,
                    info: false,
                    paging: false,
                    sort: false,
                    serverSide: false,
                    data: $this.roles,
                    lengthChange: false,
                    destroy: true,
                    columns: [
                        {
                            data: "Name",
                            title: "Name"
                        },
                        {
                            data: "Remark",
                            title: "Remark"
                        },
                        {
                            data: "Id",
                            title: "Operation",
                            render: function (data, type, full, meta) {
                                var html = '<div class="btn-table-op"><button data-toggle="modal" data-target="#modal-role" onclick="MyVue.BtnClick('+data+')" type="button" class="btn btn-info btn-xs">Edit</button>' +
                                      '<a href="@Url.Action("Assign")?roleId='+data+'" class="btn btn-warning btn-xs">Assign</a>'+
                                    '<button type="button" class="btn btn-danger btn-xs" onclick="MyVue.Delete('+data+')">Delete</button></div>';

                                if (data == 1) {
                                    html = '';
                                }
                                return html;
                            }
                        }
                    ]
                });
                $("#mainTable tr").eq(0).css({ "background-color":"rgb(60, 141, 188)","color":"#FFFFFF"});
            },
            LoadData: function () {
                $this = this;
                admin.ajax({
                    url: '@Url.Action("GetData")',
                    data: '',
                    loading: true,
                    success: function (r) {
                        if (r.status == 1) {
                            $this.roles = r.data;
                            $this.IntTable();
                        }
                    }
                });
            },
            Modal: function () {

                $('#modal-role').on('show.bs.modal', function (e) {
                    var roleId = $("#role_id").val();
                    MyVue.ClearModalInput();
                    if (roleId > 0) {
                        $(".modal-title").text("Edit Role");
                        admin.ajax({
                            url: '@Url.Action("GetRoleInfo")',
                            data: { roleId: roleId },
                            loading: true,
                            success: function (r) {
                                if (r.status == 1) {
                                    MyVue.info = r.data;
                                    console.log(r.data);
                                }
                            }
                        });
                    }
                    else {
                        $(".modal-title").text("Add Role");
                        MyVue.info["Id"] = 0;
                        MyVue.info["Name"] = "";
                        MyVue.info["Remark"] = "";
                    }
                });
            },
            ClearModalInput: function () {
                $('#modal-role input').val('');
                $("#role_id").val(0);
            },
            BtnClick: function (roleId) {
                $("#role_id").val(roleId);
            },
            Save: function () {
                var info = MyVue.info;

               admin.ajax({
                            url: '@Url.Action("Save")',
                            data: { info:info},
                            loading: true,
                            success: function (r) {
                                if (r.status == 1) {
                                    $('#modal-role').modal('hide');
                                    admin.msg("Success", "Success");
                                    MyVue.LoadData();
                                }
                                else {
                                    admin.msg("Error", "Error");
                                }
                            }
                        });
            },
            Delete: function (roleId) {
                admin.confirm("Would you like to delete it?", function () {
                    admin.ajax({
                            url: '@Url.Action("Delete")',
                            data: { roleId: roleId },
                            loading: true,
                            success: function (r) {
                                if (r.status == 1) {
                                    admin.msg("Success", "Success");
                                    MyVue.LoadData();
                                }
                                else {
                                     admin.msg("Error", "Error");
                                }
                            }
                        });

                });
                  
            }
        }

    });
    </script>
    }